package cn.xin.org.controller;

import cn.xin.base.util.AjaxResult;
import cn.xin.base.util.PageList;
import cn.xin.org.domain.Department;
import cn.xin.org.query.DepartmentQuery;
import cn.xin.org.service.IDepartmentService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")

public class DepartmentController {


    @Autowired
    private IDepartmentService iDepartmentService;

    @GetMapping
    public List<Department> findAll() {
        return iDepartmentService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public Department findById(@PathVariable("id") Long id) {
        return iDepartmentService.findById(id);
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        if (null == department.getId()) {
            // 添加
            iDepartmentService.add(department);
        } else {
            // 编辑
            iDepartmentService.update(department);
        }
        return AjaxResult.me().success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        iDepartmentService.delete(id);
        return AjaxResult.me().success();
    }

    @PostMapping
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery quey) {
        return iDepartmentService.queryByPage(quey);
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        iDepartmentService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    @GetMapping("/tree")
    public List<Department> deptTree() {
        return iDepartmentService.deptTree();
    }
}
